<template>
  <Dialog
    v-model="show"
    @confirm="handleSubmit"
    @close="close"
    title="缴费审核"
    width="600px"
  >
    <el-form :model="formData" label-width="120px" ref="formRef" :rules="rules">
      <el-form-item :label="$t('paymentRecord.商家名称')">
        <div class="form-text">{{ formData.merchantName }}</div>
      </el-form-item>
      <el-form-item :label="$t('paymentRecord.商户ID')">
        <div class="form-text">{{ formData.merchantId }}</div>
      </el-form-item>
      <el-form-item :label="$t('paymentRecord.联系电话')">
        <div class="form-text">{{ formData.phone }}</div>
      </el-form-item>
      <el-form-item :label="$t('paymentRecord.缴纳金额')">
        <div class="form-text">¥{{ formData.amount }}</div>
      </el-form-item>
      <el-form-item :label="$t('paymentRecord.缴纳时间')">
        <div class="form-text">{{ formData.payTime }}</div>
      </el-form-item>
      <el-form-item :label="$t('paymentRecord.审核结果')" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio :label="2">{{ $t('paymentRecord.通过') }}</el-radio>
          <el-radio :label="3">{{ $t('paymentRecord.驳回') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item 
        :label="$t('paymentRecord.驳回原因')" 
        prop="rejectReason"
        v-if="formData.status === 3"
      >
        <el-input 
          v-model="formData.rejectReason" 
          type="textarea" 
          rows="3" 
          :placeholder="$t('paymentRecord.请输入驳回原因')"
        />
      </el-form-item>
    </el-form>
  </Dialog>
</template>

<script setup>
import { ref, watch } from "vue";
import Dialog from "@/components/dialog.vue";
import { useI18n } from "vue-i18n";

const { t } = useI18n(); 
const show = defineModel();
const emit = defineEmits(['success'])

const props = defineProps({
  data: {
    type: Object,
    default: () => ({}),
  },
});

const formData = ref({
  merchantName: "",
  merchantId: "",
  phone: "",
  amount: "",
  payTime: "",
  status: 2,
  rejectReason: "",
});

const rules = ref({
  status: [
    { required: true, message: t('paymentRecord.请选择审核结果'), trigger: "change" }
  ],
  rejectReason: [
    { 
      required: true, 
      message: t('paymentRecord.请输入驳回原因'), 
      trigger: "blur",
      validator: (rule, value, callback) => {
        if (formData.value.status === 3 && !value) {
          callback(new Error(t('paymentRecord.请输入驳回原因')))
        } else {
          callback()
        }
      }
    }
  ],
});

watch(show, (val) => {
  if (val && props.data) {
    formData.value = {
      ...props.data,
      status: 2,
      rejectReason: ""
    };
  }
});

const formRef = ref();

const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      // 这里处理提交逻辑
      emit('success', formData.value)
      close()
    }
  });
};

const close = () => {
  formData.value = {
    merchantName: "",
    merchantId: "",
    phone: "",
    amount: "",
    payTime: "",
    status: 2,
    rejectReason: "",
  };
  show.value = false;
};
</script>

<style lang="scss" scoped>
.form-text {
  line-height: 32px;
  color: #333;
}
</style>
